package com.htzz.collection.collection;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.htzz.collection.domain.MqttInfoDto;
import com.htzz.collection.domain.ResultVO;
import com.htzz.collection.service.IMqttSender;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


/**
 * 
 * mqtt反控消息下发控制器
 * @author xdz
 * @email ixudezhi@163.com
 * @date 2021-08-31 09:13:01
 */
@Slf4j
@Api(tags = "下发mqtt消息")
@RestController
@RequestMapping("/mqtt")
public class MqttController {


    /**
     * 注入发送MQTT的Bean
     */
    @Autowired
    private IMqttSender iMqttSender;

    @ApiOperation("发送反控指令")
    @PostMapping("/feedback")
    public ResultVO feedback(@Validated @RequestBody MqttInfoDto dto){
        try{
            Map<String,Object> map =new HashMap<>();
            log.info("消息内容"+dto.toString());

            String devId=dto.getDevId();
            if(StrUtil.isBlank(devId)){
                return ResultVO.error("devId 不能为空");
            }

            JSONObject json=new JSONObject();
            json.put("devId",devId);
            Map<String,Object> datas=dto.getData();
            if(datas==null){
                return ResultVO.error("数据不能为空");
            }
            JSONObject data=JSONUtil.parseObj(datas);
            json.put("data",data);

            log.info("发送指令："+JSONUtil.toJsonStr(json));
            log.info("发送指令到：/download/"+dto.getDevId());
            iMqttSender.sendToMqtt("/feedback/"+dto.getDevId(),dto.getQos(),JSONUtil.toJsonStr(json));

            log.info("发送指令成功");
            return ResultVO.ok(map);
        } catch (Exception e) {
            log.info(e.getMessage());
            return ResultVO.error("发送指令异常"+e.getMessage());
        }
    }

    @ApiOperation("发送消息到设备")
    @PostMapping("/sendToDevice")
    public ResultVO sendToDevice(@Validated @RequestBody MqttInfoDto dto){
        try{
            Map<String,Object> map =new HashMap<>();
            log.info("消息内容"+dto.toString());

            String devId=dto.getDevId();
            if(StrUtil.isBlank(devId)){
                return ResultVO.error("devId 不能为空");
            }

            JSONObject json=new JSONObject();
            json.put("devId",devId);
            Map<String,Object> datas=dto.getData();
            if(datas==null){
                return ResultVO.error("数据不能为空");
            }
            JSONObject data=JSONUtil.parseObj(datas);
            json.put("data",data);

            log.info("发送消息："+JSONUtil.toJsonStr(json));
            log.info("发送消息到：/download/"+dto.getDevId());
            iMqttSender.sendToMqtt("/download/"+dto.getDevId(),dto.getQos(),JSONUtil.toJsonStr(json));
            log.info("发送消息成功");
            return ResultVO.ok(map);
        } catch (Exception e) {
            log.info(e.getMessage());
            return ResultVO.error("发送消息成功"+e.getMessage());
        }
    }

}
